package com.example.demo.Service;

import com.example.demo.Entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisService {
    /*
    通过Qualifier来指定bean
     */
    @Autowired
//    @Qualifier(value = "redisTemplateTest")
    private RedisTemplate redisTemplate;

    public <T> void hashPut(String tableName,T object){
        redisTemplate.opsForHash().put(tableName,"test",object.toString());
        System.out.println("Object:"+object.toString());
//        redisTemplate.opsForHash().put("hashtable","testKey","testValue");
    }

    public Object hashGet(String tableName,String name){
        return redisTemplate.opsForHash().get(tableName,name);
    }

    public String hashTestGet(String tableName,String name){
        System.out.println(redisTemplate.opsForHash().get(tableName,name));
        Person person=(Person)redisTemplate.opsForHash().get(tableName,name);
        return  person.getName();
    }

    public void hashDelete(String tableName,String name){
        redisTemplate.opsForHash().delete(tableName,name);
    }

    public Person get(String name){
        return (Person)redisTemplate.opsForValue().get(name);
    }

    public void put(Person person){
        redisTemplate.opsForValue().set(person.getName(),person);
    }
}
